package com.todo.biz.application.backend.controller;


import com.todo.biz.application.convert.DictConvert;
import com.todo.biz.application.entity.bo.BaseDictBO;
import com.todo.biz.application.service.BaseDictService;
import com.todo.biz.iface.backend.DictBackendIface;
import com.todo.biz.iface.backend.request.*;
import com.todo.biz.iface.backend.response.DictDetailResp;
import com.todo.common.common.entity.response.BizResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2024/12/9 16:33
 * @description
 */
@RestController
@RequestMapping("/bd/tool/dict")
@Tag(name = "字典后台处理类", description = "字典后台处理类")
public class DictBackendController implements DictBackendIface {

    @Resource
    private BaseDictService baseDictService;


    @Override
    @Operation(summary = "插入字典")
    @PostMapping("/insertDict")
    public BizResponse<Boolean> insertDict(@RequestBody InsertDictReq req) {
        if (req == null) {
            return BizResponse.paramError("参数为空");
        }
        if (!req.baseCheck()) {
            return BizResponse.paramError("参数错误");
        }
        Boolean inserted = baseDictService.insertBaseDict(DictConvert.buildToBaseDictBO(req));
        return BizResponse.success(inserted);
    }

    @Override
    @Operation(summary = "更新字典")
    @PostMapping("/updateDict")
    public BizResponse<Boolean> updateDict(@RequestBody UpdateDictReq req) {
        if (req == null) {
            return BizResponse.paramError("参数为空");
        }
        if (!req.baseCheck()) {
            return BizResponse.paramError("参数错误");
        }
        Boolean updated = baseDictService.updateBaseDictById(DictConvert.buildToBaseDictBO(req));
        return BizResponse.success(updated);
    }

    @Override
    @Operation(summary = "删除字典")
    @PostMapping("/removeDictById")
    public BizResponse<Boolean> removeDictById(@RequestBody RemoveDictByIdReq req) {
        if (req == null) {
            return BizResponse.paramError("参数为空");
        }
        if (!req.baseCheck()) {
            return BizResponse.paramError("参数错误");
        }
        Boolean removed = baseDictService.removeBaseDictById(req.getId());
        return BizResponse.success(removed);
    }

    @Override
    @Operation(summary = "根据id查询字典")
    @PostMapping("/queryDictById")
    public BizResponse<DictDetailResp> queryDictById(@RequestBody QueryDictByIdReq req) {
        if (req == null) {
            return BizResponse.paramError("参数为空");
        }
        if (!req.baseCheck()) {
            return BizResponse.paramError("参数错误");
        }
        BaseDictBO baseDictBO = baseDictService.queryBaseDictById(req.getId());
        return BizResponse.success(DictConvert.convertToDictDetailResp(baseDictBO));
    }

    @Override
    @Operation(summary = "根据业务code和场景code查询字典详情")
    @PostMapping("/queryDictDetail")
    public BizResponse<DictDetailResp> queryDictDetail(@RequestBody QueryDictDetailReq req) {
        if (req == null) {
            return BizResponse.paramError("参数为空");
        }
        if (!req.baseCheck()) {
            return BizResponse.paramError("参数错误");
        }
        BaseDictBO baseDictBO = baseDictService.queryBaseDictByBizCodeAndSceneCode(req.getBizCode(), req.getSceneCode());
        return BizResponse.success(DictConvert.convertToDictDetailResp(baseDictBO));
    }

    @Override
    @Operation(summary = "查询字典列表")
    @GetMapping("/queryDictDetailList")
    public BizResponse<List<DictDetailResp>> queryDictDetailList() {
        List<BaseDictBO> baseDictBOS = baseDictService.queryBaseDictListByCache();
        return BizResponse.success(DictConvert.convertToDictDetailRespList(baseDictBOS));
    }

    @Override
    @Operation(summary = "重置缓存并获取字典列表")
    @GetMapping("/queryDictListAndResetCache")
    public BizResponse<List<DictDetailResp>> queryDictListAndResetCache() {
        List<BaseDictBO> baseDictBOS = baseDictService.queryDictListAndResetCache();
        return BizResponse.success(DictConvert.convertToDictDetailRespList(baseDictBOS));
    }
}
